package com.wxzz.elearing.comment.api.app;


import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.comment.dto.AnswersDto;
import com.wxzz.elearing.comment.dto.QuestionsAnswersDto;
import com.wxzz.elearing.comment.entity.QuestionsAnswersList;
import com.wxzz.elearing.comment.service.IQuestionsAnswersListService;
import com.wxzz.elearing.comment.vo.MyAnswerVo;
import com.wxzz.elearing.comment.vo.QuestionsAnswersListVo;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 * @since 2021-05-14
 */
@RestController
@RequestMapping("/questions-answers-list")
@Api(tags = "问答类接口(小程序)")
public class WxQuestionsAnswersListController {


    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private IQuestionsAnswersListService questionsAnswersListService;

    @ApiOperation(value = "问答新增问答(小程序)", notes = "问答新增或修改问答(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "saveQuestionsAnswers", method = RequestMethod.POST)
    public ReturnVO<String> saveQuestionsAnswers(@RequestBody QuestionsAnswersList questionsAnswers, @RequestHeader(value = "token") String token) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        questionsAnswers.setPublishPerson(jsonObject.getString("nickname"));
        questionsAnswers.setPublishPersonId(jsonObject.getLong("id"));
        questionsAnswers.setIcon(jsonObject.getString("icon"));
        Long enterpriseId = jsonObject.getLong("enterpriseId");
        questionsAnswers.setEnterpriseId(enterpriseId);
        return questionsAnswersListService.saveQuestionsAnswers(questionsAnswers);
    }

    @ApiOperation(value = "问答列表(小程序)", notes = "问答列表(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "queryQuestionsAnswersList", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<QuestionsAnswersListVo>>> queryQuestionsAnswersList(@RequestBody QuestionsAnswersDto questionsAnswersDto, @RequestHeader(value = "token") String token) {
        return questionsAnswersListService.queryQuestionsAnswersList(questionsAnswersDto);
    }


    @ApiOperation(value = "问答读取草稿(小程序)", notes = "问答读取草稿(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "readDraft", method = RequestMethod.POST)
    public ReturnVO<QuestionsAnswersList> readDraft(@RequestHeader(value = "token") String token) {
        QuestionsAnswersList questionsAnswersList = new QuestionsAnswersList();
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        questionsAnswersList.setPublishPersonId( jsonObject.getLong("id"));
        questionsAnswersList.setPublishPerson(jsonObject.getString("nickname"));
        return questionsAnswersListService.readDraft(questionsAnswersList);
    }

    @ApiOperation(value = "问答我评论的问答(小程序)", notes = "问答我评论的问答(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "myAnswers", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<MyAnswerVo>>> getMyAnswers(@RequestBody AnswersDto answersDto, @RequestHeader(value = "token") String token) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        Long id = jsonObject.getLong("id");
        answersDto.setUid(id);
        return questionsAnswersListService.myAnswers(answersDto);
    }
}
